﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

/// <summary>
/// Summary description for HelperGoogleChart
/// </summary>
public class HelperGoogleChart
{
    String[] colors =
        {
            "4572a7",
            "aa4643",
            "89a54e",
            "71588f",
            "4198af",
            "db843d",
            "9999ff",
            "993366",
            "ffffcc",
            "ccffff",
            "660066",
            "ff8080",
            "0066cc",
            "ccccff"
        };
    String[] alertColors =
        {
            "4572a7",
            "aa4643",
            "89a54e",
            "71588f",
            "4198af",
            "db843d",
            "9999ff",
            "993366",
            "ffffcc",
            "ccffff",
            "660066",
            "ff8080",
            "0066cc",
            "ccccff"
        };

    Double overallMax = Double.MaxValue;

    public HelperGoogleChart()
    {
    }

    public void SetOverallMax( Double val )
    {
        overallMax = val;
    }
    public String GetRowLabels(Data.Page currentPage)
    {
        String retVal = "";

        for (int rowIndex = 0, rowCount = currentPage.GetRowCount();
            rowIndex < rowCount; rowIndex++)
        {
            retVal += "|" + currentPage.GetRowLabelValue(rowIndex);
        }
        return retVal; // "|Mar|Apr|May|June|July|" ;
    }
    public String GetColumnLabels(Data.Page currentPage)
    {
        String retVal = "chxl=0:";

        for (int numericColumnIndex = 0, numericCount = currentPage.GetNumericColumnCount();
                    numericColumnIndex < numericCount; numericColumnIndex++)
        {
            retVal += "|" + currentPage.GetNumericColumnName(numericColumnIndex);
        }

        retVal += "||" + overallMax.ToString();

        return retVal; // "||50+Kb"; 
    }

    public String GetPieLabelsColumns(Data.Page currentPage)
    {
        String retVal = "chl=";
        for (int numericColumnIndex = 0, numericCount = currentPage.GetNumericColumnCount();
                    numericColumnIndex < numericCount; numericColumnIndex++)
        {
            if (numericColumnIndex > 0)
                retVal += "|";
            retVal += currentPage.GetNumericColumnName(numericColumnIndex);
        }
        return retVal;
    }

    public String GetPieLabelsRows(Data.Page currentPage, int columnIndex)
    {
        String retVal = "chl=";
        for (int rowIndex = 0, rowCount = currentPage.GetRowCount();
                    rowIndex < rowCount; rowIndex++)
        {
            if (rowIndex > 0)
                retVal += "|";
            retVal += currentPage.GetRowLabelValue(rowIndex);

            if (columnIndex > -1)
            {
                retVal += " " + currentPage.GetNumericCellContents(rowIndex, columnIndex, true);
            }
        }
        return retVal;
    }

    public String GetMarkersColumn(Data.Page currentPage, int numericColumnIndex)
    {
        String[] markers = { "c", "d", "o", "s", "a", "v", "h", "x", "c", "d", "o", "s", "a" };  // different googlechart markers , need up to 10
        String retVal = "chm=";
        for (int rowIndex = 0, rowCount = currentPage.GetRowCount();
            rowIndex < rowCount; rowIndex++)
        {
            String alertLevel = currentPage.GetAlertLevel(rowIndex, numericColumnIndex);
            int intAlertLevel;
            int.TryParse(alertLevel, out intAlertLevel);

            if (intAlertLevel > -1 && intAlertLevel < 10)
            {
                if (rowIndex > 0)
                    retVal += "|";

                retVal += markers[intAlertLevel] + ",";
                retVal += alertColors[intAlertLevel];
                // data set index
                retVal += "," + "0";

                // rowIndex to identify the data point
                retVal += "," + rowIndex.ToString();

                // size of marker
                retVal += ",10.0";
            }
        }
        return retVal;
    }
    public String GetDataRowNames(Data.Page currentPage)
    {
        String series = "chdl=";
        for (int rowIndex = 0, rowCount = currentPage.GetRowCount();
            rowIndex < rowCount; rowIndex++)
        {
            if (rowIndex > 0)
                series += "|";
            series += currentPage.GetRowLabelValue(rowIndex);
        }
        return series;
    }

    public String GetDataRowsColors(Data.Page currentPage)
    {
        String series = "chco=";
        int colorIndex = 0;
        for (int rowIndex = 0, rowCount = currentPage.GetRowCount();
            rowIndex < rowCount; rowIndex++)
        {
            if (rowIndex > 0)
                series += ",";
            String thisColor = colors[colorIndex];

            series += thisColor;

            if (++colorIndex == colors.Length)
                colorIndex = 0;
        }
        return series;
    }
    public String GetDataColumnNames(Data.Page currentPage)
    {
        String series = "chdl=";
        for (int colIndex = 0, colCount = currentPage.GetNumericColumnCount();
                    colIndex < colCount; colIndex++)
        {
            if (colIndex > 0)
                series += "|";

            series += currentPage.GetNumericColumnName(colIndex);
        }
        return series;
    }
    public String GetDataAllRows(Data.Page currentPage)
    {
        String series = "chd=s:";
        for (int rowIndex = 0, rowCount = currentPage.GetRowCount();
            rowIndex < rowCount; rowIndex++)
        {
            if (rowIndex > 0)
                series += ",";
            series += GetNormalizedDataFromRow(currentPage, rowIndex);
        }
        return series;
    }
    public String GetDataColumns(Data.Page currentPage)
    {
        String series = "chd=s:";
        for (int colIndex = 0, colCount = currentPage.GetNumericColumnCount();
                    colIndex < colCount; colIndex++)
        {
            if (colIndex > 0)
                series += ",";

            series += GetNormalizedDataFromColumn(currentPage, colIndex);
        }
        return series;
    }
    public String GetDataColumnsColors(Data.Page currentPage) //  "chco=ff0000,00ff00";
    {
        int colorIndex = 0;

        String series = "chco=";
        for (int colIndex = 0, colCount = currentPage.GetNumericColumnCount();
                    colIndex < colCount; colIndex++)
        {
            String thisColor = colors[colorIndex];

            if (colIndex > 0)
                series += ",";
            series += thisColor;

            if (++colorIndex == colors.Length)
                colorIndex = 0;
        }
        return series;
    }

    public String GetData(Data.Page currentPage, Boolean columns, int position)
    {
        String retVal = "chd=s:"; // s:ABCDEFGHI";
        if (columns)
            retVal += GetNormalizedDataFromColumn(currentPage, position); // "s:ABCDEFGHI";
        else
            retVal += GetNormalizedDataFromRow(currentPage, position);

        return retVal;
    }

    private String GetNormalizedDataFromColumn(Data.Page currentPage, int columnIndex)
    {
        // return "chd=s:helloworld";
        String retVal = ""; // "chd=s:"; // s:ABCDEFGHI";

        Double max = overallMax;

        if (max == Double.MaxValue )
        {
            String strMax = currentPage.GetAggregateValue("column", columnIndex, "maximum", false);
            if (strMax.Length > 0)
            {
                Double.TryParse(strMax, out max );
            }
        }

        for (int rowIndex = 0, rowCount = currentPage.GetRowCount();
            rowIndex < rowCount; rowIndex++)
        {
            retVal += "";

            String strVal = currentPage.GetNumericCellContents(rowIndex, columnIndex, false);
            if (strVal.Length > 0)
            {
                Double val;
                Double.TryParse(strVal, out val );
                if (val > 0)
                    retVal += simpleEncode(val, max);
                else
                    retVal += "A";
            }
            else
                retVal += "A";
        }


        return retVal;
    }

    private String GetNormalizedDataFromRow(Data.Page currentPage, int rowIndex)
    {
        String retVal = "";

        Double max = overallMax;

        if (max == Double.MaxValue )
        {
            String strMax = currentPage.GetAggregateValue("row", rowIndex, "maximum", false);
            if (strMax.Length > 0)
            {
                Double.TryParse(strMax, out max);
            }
        }

        for (int columnIndex = 0, columnCount = currentPage.GetNumericColumnCount();
            columnIndex < columnCount; columnIndex++)
        {
            retVal += "";

            String strVal = currentPage.GetNumericCellContents(rowIndex, columnIndex, false);
            if (strVal.Length > 0)
            {
                Double val;
                Double.TryParse(strVal, out val );
                if (val > 0)
                    retVal += simpleEncode(val, max);
                else
                    retVal += "A";
            }
            else
                retVal += "A";
        }
        return retVal;
    }

    public String GetChartTitle(Data.Page currentPage, Boolean columns, int positionIndex, Boolean pageCaption, Boolean positionCaption)
    {
        String retVal = "chtt=";
        if (pageCaption)
            retVal += currentPage.GetPageCaption();

        if (positionCaption)
        {
            if (pageCaption)
                retVal += "|";
            if (columns)
            {
                retVal += currentPage.GetNumericColumnName(positionIndex);
            }
            else
            {
                retVal += currentPage.GetRowLabelValue(positionIndex);
            }
        }
        return retVal;
    }

    private String simpleEncode(Double value, Double max)
    {
        String simpleEncoding = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

        if (max > 0)
        {
            long index = (long)Math.Round((value / max) * simpleEncoding.Length - 1);
            if (index > -1 && index < simpleEncoding.Length)
            {
                if (index < int.MaxValue)
                {
                    int intIndex = (int)index;
                    return simpleEncoding.Substring(intIndex, 1);
                }
            }
        }
        return "A";
    }

    public String GetSize(String xAxis, String yAxis)
    {
        return "chs=" + xAxis + "x" + yAxis;
    }
}
